﻿using System;
using System.Globalization;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TS3QueryLib.Tests.TypeExtensions
{
    [TestClass]
    public class ExtensionMethodsTest
    {
        [TestMethod]
        public void TestChangeType()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("de-de");
            string source;

            source = "5";
            Assert.AreEqual(5, source.ChangeType<int?>());

            source = null;
            Assert.IsFalse(source.ChangeType<int?>().HasValue);

            source = "50";
            Assert.AreEqual(50, source.ChangeType<int>());

            source = "50.50";
            Assert.AreEqual(5050d, source.ChangeType<double>());

            source = "50.50";
            Assert.AreEqual(5050d, source.ChangeType<double?>());

            source = "50.50";
            Assert.AreEqual(50.50d, source.ChangeTypeInvariant<double>());

            source = "50.50";
            Assert.AreEqual(50.50d, source.ChangeTypeInvariant<double?>());

            source = "50";
            Assert.AreEqual(50u, source.ChangeType<uint>());

            source = "50";
            Assert.AreEqual(50u, source.ChangeType<ushort>());

            source = "50";
            Assert.AreEqual(50u, source.ChangeType<ulong>());

            source = "true";
            Assert.IsTrue(source.ChangeType<bool>());

            source = "false";
            Assert.IsFalse(source.ChangeType<bool>());

            source = "true";
            Assert.IsTrue(source.ChangeType<bool?>().Value);

            source = "false";
            Assert.IsFalse(source.ChangeType<bool?>().Value);

            source = "Lala";
            Assert.AreEqual(source, source.ChangeType<string>());
        }
    }
}
